package leetcode;

/**
 * @Description
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 21.5.17
 */

public class TreeNode {
    int val;
    leetcode.TreeNode left;
    leetcode.TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    public TreeNode() {
    }

    //构建完全二叉树
    public static TreeNode buildTree(int[] root, TreeNode rootNode, int index) {
        if (index >= root.length) {
            return null;
        }
        if (rootNode != null) {
            rootNode.val = root[index];
        } else {
            rootNode = new TreeNode(root[index]);
        }

        if (2 * index + 1 < root.length && root[2 * index + 1] != -1) {
            rootNode.left = new TreeNode();
            buildTree(root, rootNode.left, 2 * index + 1);
        }
        if (2 * index + 2 < root.length&& root[2 * index + 2] != -1) {
            rootNode.right = new TreeNode();
            rootNode.right = buildTree(root, rootNode.right, 2 * index + 2);
        }

        return rootNode;
    }
}
